package com.gopay.bis.manager.bank.ebank;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

public class BocomB2cManager implements BankManagerRemoteService{
	
	@Resource(name = "bocomConfig")
    private Map<String, String> bocomConfig;
	
	@Resource(name = "bocomB2cBankServiceClient")
    private BankService bocomB2cBankServiceClient;

	@Resource(name = "bocomB2cBankServiceClientHna")
    private BankService bocomB2cBankServiceClientHna;

	 
	public BocomB2cMessage sign(BaseMessage msg) throws GopayException{
		BocomB2cMessage message  = (BocomB2cMessage)msg;
		Date date = DateUtils.strToDate(message.getOrderDate(), "yyyyMMddhhmmss");
		String orderDate = DateUtils.format(date, "yyyyMMdd");
		message.setOrderDate(orderDate);
		message.setMerId(message.getMerID());
		// 交行借贷分离，不支持信用卡使用此商户号
        if (!msg.isSupportCredit()) {
            message.setMerId("301110089999542");
        }
		
		message.setInterfaceVersion(bocomConfig.get("BOCOM.B2C.INTERFACEVERSION"));
		message.setTranType(bocomConfig.get("BOCOM.B2C.TRANTYPE"));
		message.setCurType(bocomConfig.get("BOCOM.B2C.CURTYPE"));
		message.setMerUrl(message.getPayRuturnUrl());
		message.setGoodsUrl(message.getPayNotifyUrl());
		message.setNetType(bocomConfig.get("BOCOM.B2C.NETTYPE"));
		message.setNotifyType(bocomConfig.get("BOCOM.B2C.NOTIFYTYPE"));
		message.setBankUrl(message.getPayUrl());
		String plainMessage = buildPlain(message);
		message.setPlainMessage(plainMessage);
		BankLogUtils.printB2CManagerSignSendLog(message);
		String signMessage;
		if(message.isHna()){
			signMessage = (String) bocomB2cBankServiceClientHna.sign(message);
		}else{
			signMessage = (String) bocomB2cBankServiceClient.sign(message);
		}
		message.setSignMessage(signMessage);
		BankLogUtils.printB2CManagerSignReturnLog(message);
		return message;
	}
	  /**
     * B2C验签
     * 
     * @param BOCOMMessageB2C
     * @return BOCOMMessageB2C
     */
    public Object verify(BaseMessage message) throws GopayException{
        boolean flag = false;
        BankLogUtils.printB2CManagerVerifySendLog(message);
		if(message.isHna()){
			 flag = (Boolean) bocomB2cBankServiceClientHna.verify(message);
		}else{
			 flag = (Boolean) bocomB2cBankServiceClient.verify(message);
		}
        BankLogUtils.printB2CManagerVerifyReturnLog(message, flag);
        return flag;
    }
    
    /**
     * B2C
     * 
     * @param message
     * @return
     */
    public BocomB2cMessage query(Object obj)  throws GopayException{
    	BocomB2cMessage message  = (BocomB2cMessage)obj;
    	BankLogUtils.printB2CManagerQuerySendLog(message, "");
    	BocomB2cMessage b2cMessage;
		if(message.isHna()){
			b2cMessage = (BocomB2cMessage) bocomB2cBankServiceClientHna.query(message);
		}else{
			b2cMessage = (BocomB2cMessage) bocomB2cBankServiceClient.query(message);
		}
    	b2cMessage.setOutStlmCd(BankConstants.BANK_LOGO_BOCOM);
    	BankLogUtils.printB2CManagerQueryReturnLog(b2cMessage);
        return b2cMessage;
    }
	 /**
     * 构建B2C明文
     * 
     * @param BOCOMMessageB2C
     * @return String
     */
    private String buildPlain(BocomB2cMessage message) {
        StringBuffer sb = new StringBuffer();
        sb.append(message.getInterfaceVersion()).append("|");
        sb.append(message.getMerId()).append("|");
        sb.append(message.getOrderId()).append("|");
        sb.append(message.getOrderDate()).append("|");
        sb.append(message.getOrderTime()).append("|");
        sb.append(message.getTranType()).append("|");
        sb.append(message.getAmount()).append("|");
        sb.append(message.getCurType()).append("|");
        sb.append(message.getOrderContent()).append("|");
        sb.append(message.getOrderMono()).append("|");
        sb.append(message.getPhdFlag()).append("|");
        sb.append(message.getNotifyType()).append("|");
        sb.append(message.getMerUrl()).append("|");
        sb.append(message.getGoodsUrl()).append("|");
        sb.append(message.getJumpSeconds()).append("|");
        sb.append(message.getPayBatchNo()).append("|");
        sb.append(message.getProxyMerName()).append("|");
        sb.append(message.getProxyMerType()).append("|");
        sb.append(message.getProxyMerCredentials()).append("|");
        sb.append(message.getNetType());
        return sb.toString();
    }
	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}
    
}
